package com.example.trb.util.word;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.data.Pictures;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import com.example.trb.entity.ReviewForm;

import com.example.trb.entity.UserInfo;
import com.example.trb.vo.ReviewFormVO;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
public class WordUtil {

    //表格的行循环
    public static Configure TableLoop(ReviewFormVO wordInfo,Map<String,Object> map ){
        LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
        Configure config = Configure.builder()
                .bind("Education",policy)
                .bind("Paper",policy)
                .bind("WorkExperience",policy)
                .bind("PerformanceResult",policy)
                .bind("PerformancePatent",policy)
                .bind("PerformanceAward",policy)
                .build();

        map.put("Education",wordInfo.getEducation());
        map.put("Paper",wordInfo.getPaper());
        map.put("WorkExperience",wordInfo.getWorkExperience());
        map.put("PerformanceResult",wordInfo.getPerformanceResult());
        map.put("PerformancePatent",wordInfo.getPerformancePatent());
        map.put("PerformanceAward",wordInfo.getPerformanceAward());

        return config;
    }

    //在map中添加UserInfo中信息
    public static void AddInfo_User(ReviewFormVO reviewFormVO,Map<String,Object> map){
        UserInfo user = reviewFormVO.getUserAllInfo();
        map.put("name",user.getName());
        map.put("sex",user.getSex());
        map.put("birthday",user.getBirthday());
        if(user.getTwoInchPhoto() != null){
            map.put("twoInchPhoto", Pictures.ofUrl(user.getTwoInchPhoto()).size(100,100).create());
        }
        map.put("nation",user.getNation());
        map.put("origin",user.getOrigin());
        map.put("department",user.getDepartment());
        map.put("personalStatus",user.getPersonalStatus());
        map.put("address",user.getAddress());
        map.put("politicalAppearance",user.getPoliticalAppearance());
        map.put("phoneNumber",user.getPhoneNumber());
        map.put("executive",user.getExecutive());
        map.put("email",user.getEmail());
    }


    //在map中添加ReviewForm中信息
    public static void AddSummery(ReviewFormVO reviewFormVO,Map<String,Object> map){
        ReviewForm reviewForm = reviewFormVO.getReviewForm();
        map.put("participationTime",reviewForm.getParticipationTime());
        map.put("declarationSeries",reviewForm.getDeclarationSeries());
        map.put("specialty",reviewForm.getSpecialty());
        map.put("reviewYear",reviewForm.getReviewYear());
        map.put("level",reviewForm.getLevel());
        map.put("fileLocation",reviewForm.getFileLocation());
        map.put("workYear",reviewForm.getWorkYear());
        map.put("summery",reviewForm.getSummery());
    }

    public static InputStream getIs(String path) {
        ClassLoader loader = WordUtil.class.getClassLoader();
        InputStream is = loader.getResourceAsStream(path);
        return is;
    }

    //下载文件
    public static void download(OutputStream out,String templatePath,Map<String, Object> map,Configure config) throws IOException {
//        BufferedOutputStream bos = new BufferedOutputStream(out);

        // 读取模板templatePath并将paramMap的内容填充进模板，即编辑模板(compile)+渲染数据(render)
        XWPFTemplate template = XWPFTemplate.compile(templatePath,config).render(map);
        try {
            template.write(out);//将template写到OutputStream中
//            bos.flush();
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            out.close();
            template.close();
        }
    }
}

